//
//  CLInputDoubleNumber.m
//  togetherLBS
//
//  Created by 丁益 on 16/6/13.
//  Copyright © 2016年 celi. All rights reserved.
//

#import "CLInputDoubleNumber.h"
#import "CLBaseTextField.h"
#import "MBProgressHUD+MJ.h"
#import "UIColor+Hex.h"

#define MinBPM  50
#define MaxBPM  160

#define ViewWidth  sWidth*0.85
#define ViewHight  150
// 键盘高度
#define kKeyBoardHeight 216
// 键盘动画时间
#define kKeyBoardDuration 0.25
@interface CLInputDoubleNumber()<UITextFieldDelegate>

@property (nonatomic, weak) UIButton *btnCover;
@property (nonatomic, weak) UIView *inputViewBg;
@property (nonatomic, weak) UILabel *viewTitle;
@property (nonatomic, weak) UILabel *numberOneTitle;
@property (nonatomic, weak) UILabel *numberTwoTitle;
@property (nonatomic, weak) CLBaseTextField *inputTextOne;
@property (nonatomic, weak) CLBaseTextField *inputTextTwo;

@property (nonatomic, weak) UIButton *btnOk;
@property (nonatomic, weak) UIButton *btnCancel;

@end

@implementation CLInputDoubleNumber

- (id)initViewWithTitle:(NSString*)Title numberOne:(NSString*)numberOne numberTwo:(NSString*)numberTwo {

//    CGRect frame = CGRectMake((sWidth-ViewWidth)/2,(sHeight - (KeyBoard +ViewHight)-84), ViewWidth, ViewHight);

    self = [super initWithFrame:[UIScreen mainScreen].bounds];
    if(self){
    
        UIButton *btnCover = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, sWidth, sHeight)];
        [btnCover setBackgroundColor:[UIColor colorWithRed:88/255.0 green:88/255.0 blue:88/255.0 alpha:0.6]];
        [btnCover addTarget:self action:@selector(btnCoverOnclick:) forControlEvents:UIControlEventTouchUpInside];
        self.btnCover = btnCover;
        [self addSubview:btnCover];
        
        UIView *inputViewBg = [[UIView alloc]initWithFrame:CGRectMake((sWidth-ViewWidth)/2,0, ViewWidth, ViewHight)];
        inputViewBg.backgroundColor = [UIColor whiteColor];
        inputViewBg.layer.borderWidth = 1.0;
        inputViewBg.layer.cornerRadius = 5;
        inputViewBg.layer.masksToBounds = YES;
        inputViewBg.layer.borderColor = [UIColor grayColor].CGColor;
        
        //以下控件都是添加到inputViewBg上
        UILabel *viewTitle = [self creatViewLabel:Title];
        viewTitle.frame = CGRectMake(0, 0, inputViewBg.width, 30);
        viewTitle.textAlignment = NSTextAlignmentCenter;
        self.viewTitle = viewTitle;
        [inputViewBg addSubview:viewTitle];
        
        CGFloat lineHeaderViewY =  CGRectGetMaxY(viewTitle.frame);
        UIView *lineHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, lineHeaderViewY, inputViewBg.width, 1)];
        [lineHeaderView setBackgroundColor:DYColor(230, 230, 230)];
        [inputViewBg addSubview:lineHeaderView];
        
        UILabel *numberOneTitle = [self creatViewLabel:CLLocalizedString(@"MinBPM", nil)];
        self.numberOneTitle = numberOneTitle;
        CGFloat numberOneTitleY = CGRectGetMaxY(self.viewTitle.frame)+10;
        self.numberOneTitle.frame = CGRectMake(0, numberOneTitleY, 100, 30);
        self.numberOneTitle.textAlignment = NSTextAlignmentRight;
        [inputViewBg addSubview:self.numberOneTitle];
        
        UILabel *numberTwoTitle = [self creatViewLabel:CLLocalizedString(@"MaxBPM", nil)];
        self.numberTwoTitle = numberTwoTitle;
        CGFloat numberTwoTitleY = CGRectGetMaxY(self.numberOneTitle.frame)+5;
        self.numberTwoTitle.frame = CGRectMake(0, numberTwoTitleY, 100, 30);
        self.numberTwoTitle.textAlignment = NSTextAlignmentRight;
        [inputViewBg addSubview:self.numberTwoTitle];
        
        CGFloat inputTextOneX = CGRectGetMaxX(self.numberOneTitle.frame)+10;
        CGFloat inputTextOneW = ViewWidth - inputTextOneX - 10;
        CLBaseTextField *inputTextOne = [[CLBaseTextField alloc]initWithFrame:CGRectMake(inputTextOneX, self.numberOneTitle.y, inputTextOneW, 30)];
        inputTextOne.keyboardType = UIKeyboardTypeNumberPad;
        inputTextOne.delegate = self;
        inputTextOne.text = numberOne;
        [inputViewBg addSubview:inputTextOne];
        self.inputTextOne = inputTextOne;
        CLBaseTextField *inputTextTwo = [[CLBaseTextField alloc]initWithFrame:CGRectMake(self.inputTextOne.x,self.numberTwoTitle.y, inputTextOneW, 30)];;
        inputTextTwo.delegate = self;
        inputTextTwo.keyboardType = UIKeyboardTypeNumberPad;
        inputTextTwo.text = numberTwo;
        [inputViewBg addSubview:inputTextTwo];
        self.inputTextTwo = inputTextTwo;
        
        CGFloat lineTextViewY =  CGRectGetMaxY(inputTextTwo.frame) + 15;
        UIView *lineTextView = [[UIView alloc]initWithFrame:CGRectMake(0, lineTextViewY, inputViewBg.width, 1)];
        [lineTextView setBackgroundColor:DYColor(230, 230, 230)];
        [inputViewBg addSubview:lineTextView];
        
        CGFloat btnCancelWidth = 60;
        CGFloat btnCancelHight = 30;
        CGFloat btnCancelX = (inputViewBg.width)/4-btnCancelWidth/2;
        CGFloat btnCancelY = CGRectGetMaxY(inputTextTwo.frame)+20;
        UIButton *btnCancel = [[UIButton alloc]initWithFrame:CGRectMake(btnCancelX, btnCancelY, btnCancelWidth, btnCancelHight)];
        [btnCancel setTitle:CLLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
        [btnCancel setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btnCancel addTarget:self action:@selector(btnCancelOnclick:) forControlEvents:UIControlEventTouchUpInside];
        btnCancel.titleLabel.font = [UIFont systemFontOfSize:16];
        [btnCancel setTitleColor:[UIColor colorWithHexString:@"#00a5e0"] forState:UIControlStateNormal];
        self.btnCancel = btnCancel;
        [inputViewBg addSubview:btnCancel];

        CGFloat btnOkX = inputViewBg.width - (inputViewBg.width)/4 - btnCancelWidth/2;
        UIButton *btnOk = [[UIButton alloc]initWithFrame:CGRectMake(btnOkX, btnCancel.y, btnCancelWidth, btnCancelHight)];
        [btnOk setTitle:CLLocalizedString(@"OK", nil) forState:UIControlStateNormal];
        [btnOk setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btnOk addTarget:self action:@selector(btnOkOnclick:) forControlEvents:UIControlEventTouchUpInside];
        btnOk.titleLabel.font = [UIFont systemFontOfSize:16];
        [btnOk setTitleColor:[UIColor colorWithHexString:@"#00a5e0"] forState:UIControlStateNormal];
        self.btnOk = btnOk;
        [inputViewBg addSubview:btnOk];
        
        inputViewBg.height = CGRectGetMaxY(btnOk.frame) + 5;
        
        CGFloat btnLineX = inputViewBg.width/2;
        CGFloat btnLineY = lineTextView.y;
        CGFloat btnLineH = inputViewBg.height - btnLineY;
        UIView *btnLine = [[UIView alloc]initWithFrame:CGRectMake(btnLineX, btnLineY, 1, btnLineH)];
        [btnLine setBackgroundColor:DYColor(230, 230, 230)];
        [inputViewBg addSubview:btnLine];
        
        self.inputViewBg = inputViewBg;
        [self addSubview:inputViewBg];
    }
    
    return self;
}

- (UILabel*)creatViewLabel:(NSString*)title{
    UILabel *label = [[UILabel alloc]init];
    label.text = title;
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = [UIColor blackColor];
    return label;
}

- (void)showInputDoubleNumberView{
    UIView *view = [[UIApplication sharedApplication].windows lastObject];
    [view addSubview:self];
    [self.inputTextOne becomeFirstResponder];
    
    [UIView animateWithDuration:kKeyBoardDuration animations:^{
        
        self.inputViewBg.y = (sHeight - (kKeyBoardHeight +ViewHight)-84);
        self.alpha = 1.0;
    }];
}

- (void)hiddenInputDoubleNumberView{

    [self endEditing:YES];
    
    [UIView animateWithDuration:kKeyBoardDuration animations:^{
        self.inputViewBg.y = 0;
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
    
}

- (void)btnCoverOnclick:(UIButton*)btn{
    
    //退出试图
    [self hiddenInputDoubleNumberView];
}

- (void)btnCancelOnclick:(UIButton*)btn{
    
    [self hiddenInputDoubleNumberView];
}

- (void)btnOkOnclick:(UIButton*)btn{

    if ((self.inputTextOne.text.length==0)||(self.inputTextTwo.text.length==0)) {
       [MBProgressHUD showMessageAndHid:CLLocalizedString(@"InPutDateNill", nil)];
        return;
    }
    
    NSInteger numberOneValue = [self.inputTextOne.text integerValue];
    NSInteger numberTwoValue = [self.inputTextTwo.text integerValue];
    if(numberOneValue < MinBPM) {
        [MBProgressHUD showMessageAndHid:[NSString stringWithFormat:@"%@%d",CLLocalizedString(@"NotLessThan", nil),MinBPM]];
        self.inputTextOne.text = nil;
        [self.inputTextOne becomeFirstResponder];
        return;
    }
    if(numberTwoValue > MaxBPM) {
        [MBProgressHUD showMessageAndHid:[NSString stringWithFormat:@"%@%d",CLLocalizedString(@"NotGreaterThan", nil),MaxBPM]];
        self.inputTextTwo .text = nil;
        [self.inputTextTwo becomeFirstResponder];
        return;
    }
    if(numberOneValue > numberTwoValue){
        [MBProgressHUD showMessageAndHid:[NSString stringWithFormat:@"%@",CLLocalizedString(@"MinNOThanMax", nil)]];
        self.inputTextTwo .text = nil;
        [self.inputTextTwo becomeFirstResponder];
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(InputDoubleNumber:numberOne:numberTwo:)]) {
        [self.delegate InputDoubleNumber:self numberOne:self.inputTextOne.text numberTwo:self.inputTextTwo.text];
    }
    [self hiddenInputDoubleNumberView];
}

#pragma mark -UITextFieldDelegate 
- (BOOL) textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    DYLog(@"%@",string);
    return YES;
}

@end
